#include "SypEngine.h"

#ifdef CHECKMEM
#include "mmgr.h"
#endif
namespace Syp
{

	void PassRegistry::addPass(Pass* p)
	{
		addEntry(p->getName(),p);
	}

	PassRegistry* PassRegistry::pr = 0;


	void PassRegistry::removeAllEntries()
	{
		std::map<String,Pass*>::iterator b = passes.begin();
		std::map<String,Pass*>::iterator e = passes.end();
		while(b!=e)
		{
			Pass* data = b->second;
			data->destroy();
			++b;
		}
		passes.clear();


	}

	void PassRegistry::removeEntry(String& s)
	{
		getEntry(s)->destroy();
		passes.erase(s);
	};

	void PassRegistry::initialise()
	{
		if(!pr)
		{
			pr = new PassRegistry();
		}
	};

	PassRegistry& PassRegistry::getRegistry()
	{
		assert(pr && "PassRegistry has not been intialised.");
		return (*pr);
	}

	void PassRegistry::release()
	{
		ErrorLogger::getLogger()<<"Releasing PassRegistry."<<std::endl;	
		if(pr)
		{
			pr->removeAllEntries();
			delete pr;
			pr = 0;
		}
	}

	void PassRegistry::dumpInfo()
	{
		ErrorLogger::getLogger()<<"PassRegistry has "<<getNumEntries()<<" Passe(s).\n";
		dumpEntries();	

	}
}
